//
//  FragmentViewController.m
//  A_Project_DLS151129_SQ
//
//  Created by 151129-49宋强 on 16/1/22.
//  Copyright © 2016年 www.lanou3g.com蓝鸥. All rights reserved.
//

#import "FragmentViewController.h"

@interface FragmentViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) UITableView *fragmentTableView;//碎片tableView
@property (nonatomic, retain) NSMutableArray *fragmentDataArr;//碎片数据数组
@property (nonatomic, assign) NSInteger start;

@end

@implementation FragmentViewController
- (void)dealloc
{
    [_fragmentDataArr release];
    [_fragmentDataArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"碎片";
    self.view.backgroundColor = [UIColor whiteColor];
    //数据处理
    [self creatFragmentData];
    [self creatFragmentTableView];
    //初始化
    self.start = 0;
    self.fragmentDataArr = [NSMutableArray array];
    [self.view NightWithType:UIViewColorTypeNormal];
    
}
//碎片tableView
- (void)creatFragmentTableView
{
    self.fragmentTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _fragmentTableView.delegate = self;
    _fragmentTableView.dataSource = self;
    _fragmentTableView.showsHorizontalScrollIndicator = NO;
    _fragmentTableView.showsVerticalScrollIndicator = NO;
    //注册第一种cell
    [_fragmentTableView registerClass:[FragmentTableViewCellOne class] forCellReuseIdentifier:@"fragmentCell"];
    //注册第二种cell
    [_fragmentTableView registerClass:[FragmentTableViewCellTwo class] forCellReuseIdentifier:@"fragmentCellTwo"];
    [self.view addSubview:_fragmentTableView];
    //下拉刷新
    self.fragmentTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //初始化
        self.start = 0;
        [self creatFragmentData];
            }];
    //上拉加载
    self.fragmentTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
       //加载
        self.start += 10;
        [self creatFragmentData];
        [self.fragmentTableView.mj_footer endRefreshing];
    }];
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _fragmentDataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
            Model *m = _fragmentDataArr[indexPath.row];
        if (m.coverimg.length > 0) {
            FragmentTableViewCellOne * cell = [tableView dequeueReusableCellWithIdentifier:@"fragmentCell" forIndexPath:indexPath];
            [cell.headView sd_setImageWithURL:[NSURL URLWithString:m.icon]];
            cell.coverimg_wh = m.coverimg_wh;//传递图片比例参数
            cell.nameLabel.text = m.uname;
            cell.contentLabel.text = m.content;
            [cell.contentLabel NightWithType:UIViewColorTypeNormal];
            cell.timeLabel.text = m.addtime_f;
            cell.commentLabel.text = m.comment;
            cell.xinLabel.text = m.like;
            [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.coverimg]];
            [cell NightWithType:UIViewColorTypeNormal];//夜间模式
            [cell.nameLabel NightWithType:UIViewColorTypeNormal];
            [cell.timeLabel NightWithType:UIViewColorTypeNormal];
            [cell.xinLabel NightWithType:UIViewColorTypeNormal];
            [cell.commentLabel NightWithType:UIViewColorTypeNormal];
            return cell;
        }else{
            FragmentTableViewCellTwo *cellTwo = [tableView dequeueReusableCellWithIdentifier:@"fragmentCellTwo" forIndexPath:indexPath];
            cellTwo.nameLabel.text = m.uname;
            cellTwo.contentLabel.text = m.content;
            [cellTwo.contentLabel NightWithType:UIViewColorTypeNormal];
            cellTwo.timeLabel.text = m.addtime_f;
            cellTwo.commentLabel.text = m.comment;
            cellTwo.xinLabel.text = m.like;
            [cellTwo.headView sd_setImageWithURL:[NSURL URLWithString:m.icon]];
            [cellTwo NightWithType:UIViewColorTypeNormal];//夜间模式
            [cellTwo.nameLabel NightWithType:UIViewColorTypeNormal];
            [cellTwo.xinLabel NightWithType:UIViewColorTypeNormal];
            [cellTwo.commentLabel NightWithType:UIViewColorTypeNormal];
            [cellTwo.timeLabel NightWithType:UIViewColorTypeNormal];

            return cellTwo;
        }
    
    
  }
//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
        Model *m = _fragmentDataArr[indexPath.row];
        if (m.coverimg.length > 0) {
            CGFloat labelH = [FragmentTableViewCellOne heightWithText:m.content];
            CGFloat imgH = [FragmentTableViewCellOne heightWithImg:m.coverimg_wh];
            return labelH + imgH + 130;
        }else{
            CGFloat h = [FragmentTableViewCellTwo heightWithText:m.content];
            return h + 120;
        }
    
}
//fragmentTableView数据处理
- (void)creatFragmentData
{
    NSString *urlStr = @"http://api2.pianke.me/timeline/list";
    NSDictionary *dic = @{@"start": @(self.start), @"client": @2, @"limit": @10};
    [PWNetWorking POSTWithURL:urlStr body:dic completion:^(id result) {
        NSArray *arr = result[@"data"][@"list"];
        NSArray *tempArr = [Model modelHanderWithArray:arr];
        if (self.start == 0) {
        [self.fragmentDataArr removeAllObjects];
        }
        for (Model *m in tempArr) {
            [self.fragmentDataArr addObject:m];
        }
        [_fragmentTableView reloadData];
        [self.fragmentTableView.mj_header endRefreshing];
        
        
    }];
}
//cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
        Model *m = _fragmentDataArr[indexPath.row];
        ReadWebVC *webVC = [[ReadWebVC alloc] init];
        webVC.contenId = m.contentid;
        [self.navigationController pushViewController:webVC animated:YES];
        [webVC release];
    }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
